<template>
  <div id="app">
    <el-container v-if="!isLoginPage">
      <el-header style="background: #409EFF; color: white; line-height: 60px; display: flex; justify-content: space-between; align-items: center;">
        <h2>数据字典管理系统</h2>
        <div class="header-actions">
          <el-button type="text" style="color: white;" @click="handleLogout">
            <el-icon><SwitchButton /></el-icon>
            退出登录
          </el-button>
        </div>
      </el-header>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
    
    <!-- 登录页单独显示 -->
    <router-view v-else />
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import { SwitchButton } from '@element-plus/icons-vue'
import TokenUtils from '@/utils/token-utils.js'

const route = useRoute()
const router = useRouter()

// 判断当前是否是登录页
const isLoginPage = computed(() => 
  route.path === '/login'
)

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    // 清除加密的 Token 数据
    TokenUtils.clearToken()
    ElMessage.success('已退出登录')
    
    router.push('/login')
  } catch (error) {
    // 用户取消操作
  }
}
</script>